package com.jz.community.moduleshopping.orderDetail.ui;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.widget.RelativeLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.commview.utils.TriangleDrawable;
import com.jz.community.commview.view.RecycleViewDivider;
import com.jz.community.moduleshopping.R;
import com.jz.community.moduleshopping.confirmOrder.ui.NewConfirmOrderView;
import com.jz.community.moduleshopping.orderDetail.adapter.RejectOrderAdapter;
import com.zyyoona7.popup.EasyPopup;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tony.Fan on 2018/9/6 09:18
 */
public class RejectOrderPopUp {

    private EasyPopup easyPopup;

    private Context context;
    private RecyclerView reject_order_recyclerView;
    private RejectOrderAdapter rejectOrderAdapter;

    private String[] rejectOrderStr = {"我不想买了", "暂时不需要该项服务了", "信息填写错误，重新下单", "产品或服务选错了", "等待时间太长", "其他原因"};

    private List<String> rejectOrderList = new ArrayList<>();

    public interface RejectOrderListener {
        void getRejectOrderTv(String rejectText);
    }

    private RejectOrderListener rejectOrderListener;

    public void setRejectOrderListener(RejectOrderListener rejectOrderListener) {
        this.rejectOrderListener = rejectOrderListener;
    }

    public RejectOrderPopUp(Context context) {
        this.context = context;
        initPop(context);
    }

    private void initPop(Context context) {
        easyPopup = EasyPopup.create()
                .setContentView(context, R.layout.reject_order_layout)
                .setAnimationStyle(R.style.RightTop2PopAnim)
                //是否允许点击PopupWindow之外的地方消失
                .setFocusAndOutsideEnable(true)
                //允许背景变暗
                .setBackgroundDimEnable(true)
                //变暗的透明度(0-1)，0为完全透明
                .setDimValue(0.4f)
                .setOnViewListener(view -> {
                    reject_order_recyclerView = view.findViewById(R.id.reject_order_RecyclerView);
                    handleRejectOrderAdapter();
                })
                .apply();
    }

    private List<String> rejectOrderList() {
        for (int i = 0; i < rejectOrderStr.length; i++) {
            rejectOrderList.add(rejectOrderStr[i]);
        }
        return rejectOrderList;
    }

    private void handleRejectOrderAdapter() {
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        reject_order_recyclerView.setLayoutManager(layoutManager);
        rejectOrderAdapter = new RejectOrderAdapter(rejectOrderList());
        reject_order_recyclerView.addItemDecoration(new RecycleViewDivider(context, LinearLayoutManager.VERTICAL, SHelper.dp2px(context, 1), ContextCompat.getColor(context,R.color.gray_light)));
        reject_order_recyclerView.setAdapter(rejectOrderAdapter);
        rejectOrderAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                rejectOrderListener.getRejectOrderTv(rejectOrderAdapter.getData().get(position));
                easyPopup.dismiss();
            }
        });
    }

    public void showRejectOrderPopUp(View view) {
        if (easyPopup != null && easyPopup.isShowing()) {
            easyPopup.dismiss();
        } else {
            easyPopup.showAtLocation(view, Gravity.CENTER, 0, 0);
        }
    }
}
